﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

// The Templated Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234235

namespace Fabrikam.Client.Controls
{
    public sealed class WaiterControl : Control
    {
        public WaiterControl()
        {
            this.DefaultStyleKey = typeof(WaiterControl);
        }

        private ProgressBar progressBar;
        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.progressBar = (ProgressBar)this.GetTemplateChild("pgBar");

            this.SetIsInProgress((Boolean)this.GetValue(IsInProgressProperty));

        }




        public Boolean IsInProgress
        {
            get { return (Boolean)GetValue(IsInProgressProperty); }
            set { SetValue(IsInProgressProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsInProgress.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsInProgressProperty =
            DependencyProperty.Register("IsInProgress", typeof(Boolean), typeof(WaiterControl), new PropertyMetadata(false,
                IsInProgressChangedCallback));


        private  void SetIsInProgress(bool isInProgress)
        {
            this.Visibility = isInProgress ? Visibility.Visible : Visibility.Collapsed;
            this.progressBar.IsIndeterminate = isInProgress;
        }

        private static void IsInProgressChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            WaiterControl waiterControl = d as WaiterControl;
            if (waiterControl == null || waiterControl.progressBar == null)
                return;

            waiterControl.SetIsInProgress((Boolean)e.NewValue);
        }

    }
}
